package com.example.monitorsms;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public abstract class BaseActivity extends Activity {
    private static final int PERMISSIONS_CODE = 99;
    protected Activity mActivity;
    protected Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mContext = this;
        mActivity = this;
        checkPermissions();
        setStatusBar();
        main(savedInstanceState);
    }

    protected abstract int getLayoutId();

    protected void main(Bundle savedInstanceState) {
    }

    //定义需要的权限列表
    String[] permissions = new String[]{
            Manifest.permission.INTERNET,
            Manifest.permission.VIBRATE,
            Manifest.permission.READ_SMS,
            Manifest.permission.WAKE_LOCK,
            Manifest.permission.RECEIVE_SMS,
            Manifest.permission.FOREGROUND_SERVICE,
            Manifest.permission.SYSTEM_ALERT_WINDOW,
            Manifest.permission.MODIFY_AUDIO_SETTINGS,
            Manifest.permission.RECEIVE_BOOT_COMPLETED};

    /**
     * 申请权限
     */
    private void checkPermissions() {
        //如果系统大于android6.0，进行动态权限申请
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int a = ContextCompat.checkSelfPermission(this, permissions[0]);
            int b = ContextCompat.checkSelfPermission(this, permissions[1]);
            int c = ContextCompat.checkSelfPermission(this, permissions[2]);
            int d = ContextCompat.checkSelfPermission(this, permissions[3]);
            int e = ContextCompat.checkSelfPermission(this, permissions[4]);
            int f = ContextCompat.checkSelfPermission(this, permissions[5]);
            int g = ContextCompat.checkSelfPermission(this, permissions[6]);
            int h = ContextCompat.checkSelfPermission(this, permissions[7]);
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝，判断需要的权限列表中是否有权限还没拥有
            if (a != PackageManager.PERMISSION_GRANTED || b != PackageManager.PERMISSION_GRANTED ||
                    c != PackageManager.PERMISSION_GRANTED || d != PackageManager.PERMISSION_GRANTED ||
                    e != PackageManager.PERMISSION_GRANTED || f != PackageManager.PERMISSION_GRANTED ||
                    g != PackageManager.PERMISSION_GRANTED || h != PackageManager.PERMISSION_GRANTED) {
                // 如果有权限没有授予，就去提示用户请求
                startRequestPermission();
            }
        }
    }

    /**
     * 通过权限列表，提示用户赋予或禁止当前还未拥有的权限
     */
    private void startRequestPermission() {
        ActivityCompat.requestPermissions(mActivity, permissions, PERMISSIONS_CODE);
    }

    //重写该方法，用户选择后回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSIONS_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //如果用户选择禁止，此时程序没有相应权限，执行相应操作
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    //如果没有获取权限，那么可以提示用户去设置界面--->应用权限开启权限
                    Toast.makeText(mContext, "设置界面获取权限", Toast.LENGTH_SHORT).show();
                } else {
                    //如果用户同意，此时程序获取到需要的权限，执行希望的操作
                }
            }
        }
    }

    /**
     * 设置透明状态栏
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setStatusBar() {
        Window window = getWindow();
        //android6.0以后可以对状态栏文字颜色和图标进行修改
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0);
        } else {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.transparent));// 改变标题栏背景颜色
            window.setNavigationBarColor(getResources().getColor(R.color.gray));//改变标题栏字体颜色
        }
    }
}
